package org.xxpay.apibase.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class HttpKit {


    /*
     * 功能描述:
     * 生成url请求参数：  以&拼接   （参数会自动urlencoder ）
     * @param paraMap
     * @Return: java.lang.String
     * @Author: xxpay
     */
    public static String genUrlParams(Map<String, Object> paraMap) {
        if(paraMap == null || paraMap.isEmpty()) return "";
        StringBuffer urlParam = new StringBuffer();
        Set<String> keySet = paraMap.keySet();
        int i = 0;
        for(String key:keySet) {
            urlParam.append(key).append("=");
            if(paraMap.get(key) instanceof String) {
                urlParam.append(URLEncoder.encode((String) paraMap.get(key)));
            }else {
                urlParam.append(paraMap.get(key));
            }
            if(++i == keySet.size()) break;
            urlParam.append("&");
        }
        return urlParam.toString();
    }


    /*
     * 功能描述: 解析请求参数为jsonObject类型 ( 仅限简单转换， 单参数类型 )
     *
     * @param reqParamsMap
     * @Return: com.alibaba.fastjson.JSONObject
     * @Author: xxpay
     * @Date: 2020/9/18 17:17
     */
    public static JSONObject convertParamMap2JSONSimple(Map reqParamsMap) {

        JSONObject result = new JSONObject();

        for (Iterator iter = reqParamsMap.keySet().iterator(); iter.hasNext();) {
            String name = (String) iter.next();
            String[] values = (String[]) reqParamsMap.get(name);
            String valueStr = "";
            for (int i = 0; i < values.length; i++) {
                valueStr = (i == values.length - 1) ? valueStr + values[i] : valueStr + values[i] + ",";
            }
            result.put(name, valueStr);
        }

        return result;
    }




    /**
     * 发起HTTP/HTTPS请求(method=POST)
     * @param url
     * @return
     */
    public static String call4Post(String url) {
        try {
            URL url1 = new URL(url);
            if("https".equals(url1.getProtocol())) {
                return HttpClient.callHttpsPost(url);
            }else if("http".equals(url1.getProtocol())) {
                return HttpClient.callHttpPost(url);
            }else {
                return "";
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        return "";
    }


    /**
     * 发起HTTP/HTTPS请求(method=GET)
     * @param url
     * @return
     */
    public static String call4Get(String url) {
        try {
            URL url1 = new URL(url);
            if("https".equals(url1.getProtocol())) {
                return HttpClient.callHttpsGet(url);
            }else if("http".equals(url1.getProtocol())) {
                return HttpClient.callHttpGet(url);
            }else {
                return "";
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        return "";
    }


}
